﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using BartonExtension;
using SqlServerWrapper;
using ResponseObject;

namespace WebSrvs.NET.apis
{
    /// <summary>
    /// ZK_HeadIconUploadHander 的摘要描述
    /// </summary>
    public class ZK_UserActivatedHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            ZKResponse response = new ZKResponse();

            try
            {
                int uid = context.Request["uid"].toInt32().Value;
                string nickname = context.Request["nickname"];
                string subscription_brands = context.Request["s_brands"];
                string subscription_catalogs = context.Request["s_catalogs"];

                //Get File
                HttpPostedFile file = context.Request.Files["Filedata"];
                if (file == null)
                {
                    response.isProcessSuccess = false;
                    response.errMsg = "头像上传错误";
                    context.Response.Write(response.ToJson());
                    return;
                }
                string filename = DateTime.Now.DateTimeFileName() + ".jpg";
                string path = context.Server.MapPath("../data/headicons/{0}/".FormatWith(uid));
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path = path + filename;
                file.SaveAs(path);
                string connStr = SqlConnectionManager.GetConnStrFromConfig();
                using (SqlConnection conn = SqlConnectionManager.CreateSqlConnection(connStr))
                {
                    conn.Open();
                    using(SqlCommand cmd = SqlCommandManager.Create(conn,CommandType.StoredProcedure))
                    {
                        cmd.CommandText = "proc_activate_user";
                        cmd.Parameters.Add("@uid", SqlDbType.Int).Value = uid;
                        cmd.Parameters.Add("@filename", SqlDbType.VarChar).Value = filename;
                        cmd.Parameters.Add("@nickname", SqlDbType.VarChar).Value = nickname;
                        cmd.Parameters.Add("@s_brands", SqlDbType.VarChar).Value = subscription_brands;
                        cmd.Parameters.Add("@s_catalogs", SqlDbType.VarChar).Value = subscription_catalogs;
                        cmd.Parameters.Add("@return", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                        cmd.ExecuteNonQuery();

                        int returnVal = cmd.Parameters["@return"].Value.toInt32().Value;
                        if(returnVal == -1)
                        {
                            response.isProcessSuccess = false;
                            response.errMsg = "账户激活失败";
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                response.isProcessSuccess = false;
                response.errCode = ErrorCodes.CODE_ERR_INTERNAL_ERR;
                response.errMsg = ex.Message;
            }
            context.Response.Write(response.ToJson());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}